// index.js
var api = require("../../api.js");
 var app = getApp();
  var WxParse = require("../../wxParse/wxParse.js");
  var r,tips;
Page({
  data: {
    couponData: [],
    couponNo:'',
    isShow:false,
    isFromSearch: true,// 用于判断searchSongList数组是不是空数组，默认true，空的数组 
    searchLoadingComplete: false  //“没有数据”的变量，默认false，隐藏  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    r =this;
    r.setData({ 
          searchSongList:[],  //放置返回数据的数组,设为空  
          isFromSearch: true,  //第一次加载，设置true  
          searchLoading: true,  //把"上拉加载"的变量设为true，显示  
          searchLoadingComplete:false //把“没有数据”设为false，隐藏  
      })  
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
     })
    
    var that = this;
    r.toast = that.selectComponent("#toast");
    tips = that.selectComponent("#mytips");
    if (getApp().globalData.userInfo != null) {
      // 高进用户全益
      r.initData();
    };


  },
  initData:function(){
    var t= new Object();
    t.openId = getApp().globalData.wxUserInfo.openId;
    wx.request({
      url: api.mine.mineCoupon,
      data:t,
      success: function (res) {
        if (res.statusCode == 200) {
          if(res.data.code==200){
            var n = res.data.data;
            if(n.length!=0){
              var checkStatus='';
              n.length<10?checkStatus=false:true; 
              console.log(checkStatus)
             setTimeout(()=>{
              r.setData({
                couponData: n,
                loading:checkStatus,
                loadingComplete:checkStatus,
                hidden:false
              });
             },500);
             wx.hideLoading();
            }else{
              wx.hideLoading();
              setTimeout(()=>{
                r.setData({
                  loadingComplete:false,
                  loading:false,
                  hidden:true
                })
              },500)
            }
        }

      }
    }
    });
  },
  listenerNewLabelInput:function(e){
    r.data.couponNo = e.detail.value;
  },

  changeNum(e){
    let couponNo = r.data.couponNo;
    if (r.data.couponNo == ''){
        tips.showTopTip('请输入优惠卷码');
        return;
      
    }else{
      var t = new Object();
      t.couponNo =r.data.couponNo, t.openId = getApp().globalData.wxUserInfo.openId;
      wx.request({
        url: api.mine.checkCoupon,
        data:t,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          if (res.statusCode == 200) {
            if(res.data.code==200){
                  r.initData();
            }else{
              r.toast.showToast(res.data.msg);
            }
          }

        }
      })
    }

  },

})